import { Component, OnInit, ViewChild, inject } from '@angular/core';
import { FormBuilder } from '@angular/forms';
import { STColumn, STComponent } from '@delon/abc/st';
import { SFComponent, SFSchema, SFSelectWidgetSchema } from '@delon/form';
import { ModalHelper, _HttpClient } from '@delon/theme';
import { SHARED_IMPORTS } from '@shared';
import { BaseService } from 'src/app/core/utils/base.service';
import { TagSelectComponent } from '@delon/abc/tag-select';

const addTitle = '新增收费项目';
const editTitle = '修改收费项目';

@Component({
  selector: 'app-pub-item-index',
  standalone: true,
  imports: [...SHARED_IMPORTS, TagSelectComponent],
  templateUrl: './index.component.html'
})
export class PubItemIndexComponent implements OnInit {
  url = `/api/dc/pub-item`;
  chrgitmTypeGrpUrl = `/api/dc/chrgitm-type-grp`;
  chrgitmTypeUrl = `/api/dc/chrgitm-type`;
  @ViewChild('sf', { static: false }) private sf!: SFComponent;
  chrgitmTypeGrpList: any;
  chrgitmTypeList: any;
  editVisible = false;
  detailTitle = '';
  chargeItem: any;

  searchSchema: SFSchema = {
    properties: {
      key: {
        type: 'string',
        title: '关键字'
      }
    }
  };

  @ViewChild('st') private readonly st!: STComponent;
  columns: STColumn[] = [
    { title: '收费类别', index: 'chargeItemTypeLabel' },
    { title: '编号', index: 'chargeItemValue' },
    { title: '名称', index: 'chargeItemLabel' },
    { title: '单价', index: 'price' },
    { title: '规格', index: 'spec' },
    { title: '单位', index: 'unit' },
    { title: '有效性', index: 'valid', type: 'enum', enum: { true: '有效', false: '无效' } },
    {
      title: '',
      buttons: [{ text: '编辑', click: (item: any) => this.edit(item) }]
    }
  ];

  public editForm = this.fb.group({
    medChrgitmType: [],
    chargeItemTypeValue: [],
    chargeItemTypeLabel: [],
    chargeItemLabel: [],
    price: [],
    priceCode: [],
    spec: [],
    unit: [],
    valid: []
  });

  constructor(
    private http: _HttpClient,
    private modal: ModalHelper,
    private fb: FormBuilder,
    public baseService: BaseService
  ) {}

  ngOnInit(): void {
    this.getChrgitmTypeGrp();
    this.getChrgitmType();
  }

  // /**
  //  * ocean 2023-4-17
  //  * 获取收费项目分页列表
  //  */
  // getChargeItems() {
  //   this.http.get('/api/pay/charge-item').subscribe(res=>{
  //     this.chargeItemList = res;
  //   });
  // }

  changeTypeGrp(chrgitmTypeGrpCode: String): void {}

  changeType(chrgitmTypeCodg: String): void {}

  getChrgitmTypeGrp() {
    this.http.get(this.chrgitmTypeGrpUrl).subscribe(res => {
      this.chrgitmTypeGrpList = res.items;
    });
  }

  getChrgitmType() {
    this.http.get(this.chrgitmTypeUrl).subscribe(res => {
      this.chrgitmTypeList = res.items;
    });
  }

  /**
   * ocean 2023-4-18
   * 收费类别选择
   */
  selecteOptionChange(value: any) {
    // if (this.medChrgitmTypeList) {
    //   const data = this.medChrgitmTypeList.find((m: { value: any }) => m.value == value);
    //   if (data) {
    //     this.editForm.patchValue({
    //       chargeItemTypeLabel: data.label
    //     });
    //   }
    // }
  }

  add() {
    this.editForm.reset();
    this.editVisible = true;
    this.detailTitle = addTitle;
    this.chargeItem = null;
  }

  edit(e: any) {
    this.chargeItem = e;
    this.editForm.patchValue(e);
    this.editVisible = true;
    this.detailTitle = editTitle;
  }

  close() {
    this.editVisible = false;
  }

  /**
   * ocean 2023-4-17
   * 保存
   */
  save() {
    if (this.baseService.hasError(this.editForm)) {
      return;
    }

    const url = '/api/pay/charge-item';
    const formValue = this.editForm.getRawValue();

    if (this.detailTitle == addTitle) {
      // 新增
      this.http.post(url, formValue).subscribe(async res => {
        this.editVisible = false;
        this.editForm.reset();
        this.baseService.showMessage({ messageId: 'C0003' });
        this.st.reload();
      });
    } else {
      // 修改
      this.http.put(`${url}/${this.chargeItem.id}`, formValue).subscribe(async res => {
        this.editVisible = false;
        this.editForm.reset();
        this.baseService.showMessage({ messageId: 'C0003' });
        this.st.reload();
      });
    }
  }
}
